import paho.mqtt.client as mqtt

def on_connect(client, userdata, flags, rc):
    print("Connected with result code: " + str(rc))

def on_message(client, userdata, msg):
    print(msg.topic + " " + str(msg.payload))

def int16arr_to_bytearray(x):
    result = []
    for item in x:
        result.append(item & 0xff)
        result.append(item >> 8)

    return bytearray(result)

gree_off = [8950, 4450,\
                650, 550, 650, 1600, 700, 500, 700, 500, 650, 1650, 650, 550, 650, 550, 650, 550,\
                650, 550, 650, 500, 700, 500, 650, 1650, 650, 550, 650, 550, 650, 550, 650, 550,\
                650, 550, 650, 500, 700, 500, 650, 550, 650, 550, 650, 1650, 650, 550, 650, 550,\
                650, 500, 700, 500, 700, 500, 650, 550, 650, 1650, 650, 550, 650, 1600, 700, 500,\
                650, 550, 650, 1650, 650, 550,\
                650, 19750,\
                700, 500, 700, 500, 700, 500, 650, 550, 650, 550, 650, 550, 650, 550, 650, 550,\
                650, 550, 650, 550, 650, 500, 700, 500, 650, 550, 650, 550, 650, 550, 650, 550,\
                650, 550, 650, 550, 650, 550, 650, 500, 700, 500, 700, 500, 650, 550, 650, 550,\
                650, 550, 650, 550, 650, 550, 650, 550, 650, 500, 700, 500, 700, 1600, 650, 550,\
                650, 39600, 8900, 4450,\
                650, 550, 650, 1600, 700, 550, 650, 500, 700, 1600, 650, 550, 650, 550, 650, 550,\
                650, 550, 650, 550, 650, 500, 700, 1600, 650, 550, 650, 550, 650, 550, 650, 550,\
                650, 550, 650, 550, 650, 500, 700, 500, 700, 500, 650, 1650, 650, 550, 650, 550,\
                650, 550, 650, 550, 650, 500, 700, 500, 650, 1650, 650, 1650, 650, 1600, 700, 500,\
                650, 550, 650, 1650, 650, 550,\
                650, 19750,\
                700, 500, 700, 500, 700, 500, 650, 550, 650, 550, 650, 550, 650, 550, 650, 550,\
                650, 550, 650, 550, 650, 500, 700, 500, 650, 550, 650, 550, 650, 550, 650, 550,\
                650, 550, 650, 550, 650, 550, 650, 500, 700, 1600, 650, 550, 650, 550, 650, 550,\
                650, 550, 650, 550, 650, 550, 650, 500, 700, 1600, 650, 550, 650, 1650, 650, 550,\
                650]

gree_on = [8900, 4450, \
                650, 550, 650, 1600, 700, 550, 650, 1600, 650, 1650, 650, 550, 650, 550, 650, 550, \
                650, 550, 650, 500, 700, 500, 700, 1600, 650, 550, 650, 550, 650, 550, 650, 550, \
                650, 550, 650, 500, 700, 500, 650, 550, 650, 550, 650, 1650, 650, 550, 650, 550, \
                650, 500, 700, 500, 700, 500, 650, 550, 650, 1650, 650, 550, 650, 1600, 700, 500, \
                700, 500, 650, 1650, 650, 550, \
                650, 19750, \
                700, 500, 700, 500, 700, 500, 650, 550, 650, 550, 650, 550, 650, 550, 650, 550, \
                650, 550, 650, 500, 700, 500, 700, 500, 650, 550, 650, 550, 650, 550, 650, 550, \
                650, 550, 650, 550, 650, 550, 650, 500, 700, 500, 650, 550, 650, 550, 650, 550, \
                650, 550, 650, 550, 650, 550, 650, 550, 650, 500, 700, 500, 700, 1600, 650, 1650, \
                650, 39600, 8900, 4400, \
                700, 500, 700, 1600, 650, 550, 650, 1650, 650, 1600, 700, 550, 650, 500, 650, 550, \
                650, 550, 650, 550, 650, 550, 650, 1600, 700, 550, 650, 550, 650, 500, 650, 550, \
                650, 550, 650, 550, 650, 550, 650, 550, 650, 550, 650, 1600, 700, 500, 650, 550, \
                650, 550, 650, 550, 650, 550, 650, 550, 650, 1600, 700, 1600, 650, 1650, 650, 550, \
                650, 550, 650, 1600, 650, 550, \
                650, 19800, \
                650, 550, 650, 550, 650, 550, 650, 550, 650, 550, 650, 500, 700, 500, 650, 550, \
                650, 550, 650, 550, 650, 550, 650, 550, 650, 550, 650, 550, 650, 500, 700, 500, \
                650, 550, 650, 550, 650, 550, 650, 550, 650, 1600, 700, 500, 700, 500, 650, 550, \
                650, 550, 650, 550, 650, 550, 650, 550, 650, 1600, 700, 500, 700, 1600, 650, 1650, \
                650]

def gree_ac(topic, cmd):
    mqtt_server = 'gzhwedu.cn'
    mqtt_port = 1883

    client = mqtt.Client(client_id = "python-client")
    client.on_connect = on_connect
    client.on_message = on_message
    client.connect(mqtt_server, mqtt_port, 600) # 600为keepalive的时间间隔

    if cmd == 'on':
        client.publish(topic, payload=int16arr_to_bytearray(gree_on), qos=0)
    elif cmd == 'off':
        client.publish(topic, payload=int16arr_to_bytearray(gree_off), qos=0)
    # client.publish('hw/home/wol', payload=b'\xf0\x2f\x74\xdd\x98\x96', qos=0)

def wakeupPC(topic, mac):
    mac = mac.replace(':', '')

    mqtt_server = 'gzhwedu.cn'
    mqtt_port = 1883

    client = mqtt.Client(client_id = "python-client")
    client.on_connect = on_connect
    client.on_message = on_message
    client.connect(mqtt_server, mqtt_port, 600) # 600为keepalive的时间间隔
    

    client.publish(topic, payload=bytes.fromhex(mac), qos=0)

if __name__ == '__main__':
    gree_ac ('home/202/ir/1', 'off')